package com.koron.order.overhaul.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.order.experiment.bean.vo.ExperimentVo;
import com.koron.order.overhaul.bean.dto.WorkTicket;
import com.koron.order.overhaul.bean.entity.OverhaulOrderBean;
import com.koron.order.overhaul.bean.entity.OverhaulOrderMajorFormBean;
import com.koron.order.overhaul.bean.query.OverhaulExperimentQuery;
import com.koron.order.overhaul.bean.query.OverhaulOrderMajorFormQuery;
import com.koron.order.overhaul.bean.query.OverhaulOrderQuery;
import com.koron.order.overhaul.bean.vo.OverhaulOrderDetailVO;
import com.koron.order.overhaul.bean.vo.OverhaulOrderMajorFormVO;
import com.koron.order.overhaul.filter.OverhaulFilter;
import com.koron.order.overhaul.service.OverhaulOrderMajorFormService;
import com.koron.order.overhaul.service.OverhaulOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * (OverhaulOrderBean)表控制层
 * @author twcao
 * @since 2021-09-29 09:49:24
 */
@Slf4j
@Api(tags = "大修工单")
@RestController
@RequestMapping("/overhaul")
public class OverhaulOrderController implements WorkflowController {

    EamUser getCurrUser(){
        return ThreadLocalContext.get();
    }

    @Autowired
    private OverhaulOrderService overhaulOrderService;

    @Autowired
    private OverhaulOrderMajorFormService formService;

    @Autowired
    private OverhaulFilter filter;

    @ApiOperation(value = "根据大修工单id查询详情")
    @GetMapping("queryById")
    @ApiParam(name = "id", value = "大修工单id")
    public Response<OverhaulOrderDetailVO> queryById(String id) {
        log.info("大修工单详情查询， id = {}", id);
        return Response.ok("查询成功", ADOConnection.runTask(getCurrUser().getCurrDs(), overhaulOrderService, "queryById", OverhaulOrderDetailVO.class, id));
    }

    @DeleteMapping("deleteById")
    @ApiOperation(value = "根据id删除大修工单")
    @ApiParam(value = "大修工单id")
    public Response<OverhaulOrderDetailVO> deleteById(@CasUser @ApiIgnore EamUser user, String id) {
        log.info("大修工单删除，id = {}", id);
        return Response.ok("删除成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "deleteById", OverhaulOrderDetailVO.class, id));
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("queryList")
    public Response<PageInfo<OverhaulOrderBean>> queryList(@CasUser @ApiIgnore EamUser user, OverhaulOrderQuery query) {
        log.info("大修工单列表查询，query = {}", JSON.toJSONString(query));
        OrderByInterceptor.setOrderBy(query.getOrders());
        //PageHelper.startPage(query.getPage(), query.getPageSize(), query.getOrderBy());
        List list = ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "queryList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @ApiOperation(value = "根据作业表单id查询详情")
    @GetMapping("/form/getById")
    @ApiParam(name = "formId", value = "表单id")
    public Response<OverhaulOrderMajorFormVO> getFormById(@CasUser @ApiIgnore EamUser user, @RequestParam String formId) {
        log.info("根据作业表单id查询详情，formId = {}", formId);
        OverhaulOrderMajorFormVO formVO = ADOConnection.runTask(user.getCurrDs(), formService, "queryDetailById", OverhaulOrderMajorFormVO.class, formId);
        return Response.ok("查询成功", formVO);
    }

    @ApiOperation(value = "查询表单列表-共关联试验工单列表使用")
    @GetMapping("/form/queryExperimentVoList")
    public Response<PageInfo<ExperimentVo>> queryExperimentVoList(@CasUser @ApiIgnore EamUser user, OverhaulExperimentQuery query) {
        log.info("查询表单列表-共关联的试验工单使用，query = {}", JSON.toJSONString(query));
        Assert.hasText(query.getOverhaulId(), "工单id不能为空");
        //PageHelper.startPage(query.getPage(), query.getPageSize(), query.getOrderBy());
        List list = ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "queryExperimentVoList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @ApiOperation(value = "查询表单列表-共分级分项验收单使用")
    @GetMapping("/form/queryList")
    public Response<PageInfo<OverhaulOrderMajorFormBean>> formQueryList(@CasUser @ApiIgnore EamUser user, OverhaulOrderMajorFormQuery query) {
        log.info("查询表单列表-共分级分项验收单使用，query = {}", JSON.toJSONString(query));
        Assert.hasText(query.getOverhaulId(), "工单id不能为空");
        PageHelper.startPage(query.getPage(), query.getPageSize(), query.getOrderBy());
        List list = ADOConnection.runTask(user.getCurrDs(), formService, "queryList", List.class, query);
        return Response.ok("查询成功", new PageInfo<>(list));
    }

    @ApiOperation(value = "根据工单id查询关联的工作票状态列表")
    @GetMapping("/ticket/queryTicketByOrderId")
    public Response<List<Integer>> queryTicketByOrderId(@CasUser @ApiIgnore EamUser user, String orderId) {
        log.info("根据工单id查询关联的工作票，orderId = {}", orderId);
        List list = ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "queryTicketByOrderId", List.class, orderId);
        return Response.ok("查询成功", list);
    }

    @ApiOperation(value = "根据工单id查询关联的工作票列表")
    @GetMapping("/ticket/queryTicketListByOrderId")
    public Response<List<WorkTicket>> queryTicketListByOrderId(@CasUser @ApiIgnore EamUser user, String orderId) {
        log.info("根据工单id查询关联的工作票，orderId = {}", orderId);
        List list = ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "queryTicketListByOrderId", List.class, orderId);
        return Response.ok("查询成功", list);
    }

    @ApiOperation(value = "可提交人员列表查询")
    @GetMapping("allowSubmit")
    @ApiParam(name = "orderId", value = "大修工单id")
    public Response<List<String>> allowSubmit(@CasUser @ApiIgnore EamUser user, @RequestParam("orderId") String orderId) {
        log.info("可提交人员列表查询，orderId = {}", orderId);
        return Response.ok("查询成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "allowSubmit", List.class, orderId));
    }

    @ApiOperation(value = "提交")
    @PostMapping("/submit")
    public Response<String> submit(@CasUser @ApiIgnore EamUser user, @RequestBody OverhaulOrderBean bean) {
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "updateStatus", String.class, bean));
    }

    @ApiOperation(value = "单个作废工单")
    @DeleteMapping("deprecated")
    public Response<String> deprecated(String orderId) {
        Assert.isTrue(StringUtils.isNotEmpty(orderId));
        return Response.ok(ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), overhaulOrderService, "deprecated", String.class, orderId));
    }

    @Override
    @PostMapping("/workflowUpdate")
    @ApiOperation("修改业务状态, 非前端接口")
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("业务单: {}, 工作流节点提交", JSON.toJSONString(redirectQuery));
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), overhaulOrderService, "workflowUpdate", Boolean.class, redirectQuery);
        return Response.success();
    }

    @Override
    @PostMapping("/workflowUpdateEnd")
    @ApiOperation("结束时的修改方法, 非前端接口")
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        JSONObject formObj = JSON.parseObject(redirectQuery.getFormVariables());
        String orderId = formObj.getString("id");
        log.info("业务单id: {}, 工作流节点提交", orderId);
        return Response.success(ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), overhaulOrderService, "toAcceptFinish", Boolean.class, orderId));
    }

    @Override
    @PostMapping("/workflowUpdateCancel")
    @ApiOperation("作废时的修改方法, 非前端接口")
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        JSONObject formObj = JSON.parseObject(redirectQuery.getFormVariables());
        String orderId = formObj.getString("id");
        log.info("业务单id: {}, 工作流作废", orderId);
        return Response.success();
    }

    /**
     * 自动过滤
     * 点击工作流提交的时候，业务需要编写此过滤器，返回过滤后的人员信息
     * @param submitterParam
     */
    @PostMapping("/{taskN}/submitter")
    public Response<List<WorkflowUser>> submitter(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("大修工单设置审批人入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Submitter", List.class, submitterParam);
        return Response.ok(users);
    }

    /**
     * 选人组件过滤
     * 点击选人组件时，业务需要编写此过滤器，返回过滤后的人员信息
     *
     * @param submitterParam
     */
    @PostMapping("/{taskN}/handler")
    public Response<List<WorkflowUser>> handler(@PathVariable String taskN, @RequestBody SubmitterParam submitterParam) {
        log.info("大修工单选人组件过滤入参: {}", JSON.toJSONString(submitterParam));
        List<WorkflowUser> users = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), filter, taskN + "Handler", List.class, submitterParam);
        return Response.ok(users);
    }
}
